﻿namespace Jsl.StyleCop.CSharp.RefactoringRules
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class TestMoveVariableDeclarationToUsageLevel : TestBase<RuleAnalyzer>
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="TestMoveVariableDeclarationToUsageLevel"/> class.
        /// </summary>
        public TestMoveVariableDeclarationToUsageLevel()
            : base(RuleAnalyzer.MoveVariableDeclarationToUsageLevel)
        {
        }
        #endregion Constructors

        [TestMethod]
        public void Variable_is_used_at_the_same_level_as_declared()
        {
            this.TestMethod(
@"public int Method()
{
    int variable = 5;
    return variable;
}");
        }

        [TestMethod]
        public void Variable_should_be_moved_into_true_part_of_if_statement()
        {
            this.TestMethod(
@"public int Method()
{
    int variable = 5;
    if (true)
    {
        return variable;
    }
}",
                new ViolationInfo(3, "The 'variable' variable declaration should be moved to the code block where it is used. The code block starts at line 4."));
        }

        [TestMethod]
        public void Variable_used_inside_a_for_where_variable_is_defined_in_the_for_statement_is_OK()
        {
            this.TestMethod(
@"public int Method()
{
    for (int i = 0; i < 10; i++)
    {
        return i;
    }
}");
        }
    }
}